
package com.wangzha.business.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;
import com.wangzha.common.base.service.BaseService;
import com.wangzha.common.kit.SqlKit;
import com.wangzha.common.model.Record_recordGroup;
import com.wangzha.common.vo.Grid;
import org.apache.commons.collections.CollectionUtils;

import java.util.List;

/**
 * @author qinhailin
 * @date 2021-03-25
 */
public class Record_recordGroupService extends BaseService {
    private Record_recordGroup dao = new Record_recordGroup().dao();

    @Override
    public Model<Record_recordGroup> getDao() {
        return dao;
    }

    public Grid page(int pageNumber, int pageSize, Record record) {
        Record rd = new Record();
        //rd.set("user_name like", record.getStr("keyword"));
        return queryForList(pageNumber, pageSize, rd);
    }

    public List<Record_recordGroup> findByGroupId(Long groupId) {
        return dao.find("select * from " + getTable() + " where record_group_id = ? order by id", groupId);
    }

    public List<Record_recordGroup> findByGroupIds(List<Long> groupIds) {
        if (CollectionUtils.isEmpty(groupIds)) {
            return null;
        }
        return getDao().find("select * from "+ getTable() +" where record_group_id in " + SqlKit.joinId(groupIds)+" order by id");
    }

    public boolean deleteByGroupIdAndRecordId(Long groupId, Long recordId) {
        String sql = "delete from " + getTable() + " where record_group_id = ? and record_id = ? ";
        return Db.update(sql, groupId, recordId) > 0 ? true : false;
    }

    public Record_recordGroup findByGroupIdAndRecordId(Long groupId, Long recordId) {
        String sql = "select * from " + getTable() + " where record_group_id = ? and record_id = ? ";
        return dao.findFirst(sql, groupId, recordId);
    }

    public List<Record_recordGroup> findByGroupIdAndRecordIds(Long groupId, List<String> recordIds) {
        String sql = "select * from " + getTable() + " where record_group_id = " + groupId + " and record_id in " + SqlKit.joinIds(recordIds);
        return dao.find(sql);
    }

    public boolean saveRecord_recordGroups(List<Record_recordGroup> record_recordGroups){
        Db.batchSave(record_recordGroups, record_recordGroups.size());
        return true;
    }
}

